package org.springblade.modules.serial;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class NettyServerInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) {
        ChannelPipeline pipeline = ch.pipeline();


		// 在初始化器中添加
		pipeline.addLast("idleStateHandler", new IdleStateHandler(0, 30, 0, TimeUnit.SECONDS));
		pipeline.addLast("timeoutHandler", new WriteTimeoutDetectionHandler());
//		pipeline.addLast("heartbeatHandler", new HeartbeatHandler());
//		pipeline.addLast("heartbeatHandler", new SmartWriteTimeoutHandler());


		// 添加编解码器
		pipeline.addLast("decoder", new StringDecoder());
		pipeline.addLast("encoder", new StringEncoder());

		// 添加业务处理器
		pipeline.addLast("handler", new NettyServerHandler());
		pipeline.addLast(new CustomerInboundHandler());
    }
}
